<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
channel 1
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

func main() {
	c := make(chan int, 1)
	for done := false; !done; {
		select {
		default:
			print(1)
			done = true
		case &lt;-c:
			print(2)
			c = nil
		case c &lt;- 1:
			print(3)
		}
	}
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">
<input type="radio" id="choiceC" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">321</label></li>
<li><label for="choiceB">21</label></li>
<li><label for="choiceC">1</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: 321
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/eR8C9vzWIwH">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
For the 1st loop step, only the operation <code class="tmd-code-span">c &lt;- 1</code> is non-blocking. So the last <code class="tmd-code-span">case</code> branch is chosen.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
For the 2nd loop step, only the operation <code class="tmd-code-span">&lt;-c</code> is non-blocking. So the first <code class="tmd-code-span">case</code> branch is chosen.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
For the 3rd loop step, both the channel operations are blocking. So the <code class="tmd-code-span">default</code> branch is chosen.
</div>
</li>
</ul>
<p></p>
</div>
<p></p>
</div>
